package com.anysoft.web;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.webkit.WebView;

import com.anysoft.WebViewNetworkProtocol;
import com.anysoft.event.FundBuyResultEvent;
import com.anysoft.event.TimeOutEvent;
import com.anysoft.me.base.BaseWebViewActivity;
import com.anysoft.utils.CommonUtil;
import com.anysoft.utils.MentorLogger;
import com.anysoft.utils.StringUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 定投结果月诶an
 * Created by wwq on 2017/3/16.
 */

public class FundInvestResultActivity extends BaseWebViewActivity {
    private String url = "";
    private boolean isTheLastPage = false;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isShowTitleBar(true);
        leftButton.setVisibility(View.GONE);
        ivLeftButton.setVisibility(View.GONE);
        title.setText("定投结果");

        if(getIntent().hasExtra("url")) {
            url = getIntent().getStringExtra("url");
        }
        if(StringUtils.isNullOrEmpty(url)){
            finish();
        }
        isTheLastPage = true;
        loadUrl = url;
        if (CommonUtil.isNetWorkConnected(this)) {
            setWebViewConfig(loadUrl);
        }
    }
    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onshouldOverrideUrlLoadingCallBack(WebView webview, String url) {
        MentorLogger.Log(TAG, "url=" + url);
        if (url.contains("prompt.html")){
            mIntent.setClass(mActivity, SelectWarmPromptActivity.class);
            mIntent.putExtra("url",url);
            startActivity(mIntent);
        }else if (url.contains(WebViewNetworkProtocol.GOTO_FUND_LIST)) {
            EventBus.getDefault().post(new FundBuyResultEvent(true));
            finish();
        } else {
            webview.loadUrl(url);
        }
    }

    @Override
    public void onBackPressed() {
//        if (isTheLastPage) {
//            EventBus.getDefault().post(new FundBuyResultEvent(true));
//        }
        super.onBackPressed();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGestureSetCallBack(FundBuyResultEvent event) {
        if (event.isSuccess) {
            finish();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTimeOutCallBack(TimeOutEvent event) {
        if (event.isSuccess) {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
